home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / SORTIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  2.3 KB  |  83 lines

  1. /*  sortio.c - I/O functions for sorttext program */
  2. /*  do_open , do_close , getrec , putrec */
  3. #include   "stdio.h"
  4. #include   "cminor.h"
  5.  
  6. FILE *gfopen() ;
  7.  
  8. FILE *do_open(fname,fmode)    /* open a file and check for errors */
  9.   char    fname[] ;        /* file name */
  10.   char    fmode[] ;        /* read/write/append mode */
  11.   {                /* return a file pointer */
  12.      FILE  *fd ;
  13.  
  14.      fd = gfopen(fname,fmode,ASC_MODE) ;
  15.      if( fd == NULL )
  16.     {  printf("\n can't open file - %s \n",fname) ;
  17.        exit( 8 ) ;
  18.     }
  19.      return( fd ) ;
  20.   }
  21.  
  22. int do_close(fd,fname)        /* close file and check for errors */
  23.   FILE    *fd ;            /* file pointer */
  24.   char    fname ;         /* name of file being closed */
  25.   {
  26.      if( fclose(fd) < 0 )
  27.     {  printf("\n can't close file - %s \n",fname) ;
  28.        exit( 10 ) ;
  29.     }
  30.   }
  31.  
  32.  
  33. int getrec(rec,maxr,fd)
  34.   char    rec[] ;         /* put it here in string form */
  35.   int    maxr ;            /* maximum length permitted */
  36.   FILE     *fd ;            /* file pointer to input file */
  37.   {                /* return no. of chars used in rec */
  38.      return( getl(rec,maxr,fd));/* let getl do the work */
  39.   }
  40.  
  41.  
  42. int  putrec(rec,fd)        /* output one record */
  43.   char    rec[] ;         /* the record to output */
  44.   FILE    *fd ;            /* output file pointer */
  45.   {
  46.      return( putl(rec,fd) ) ;    /* use putl to do line output */
  47.   }
  48.  
  49.  
  50. /* ******************************* line oriented functions */
  51.  
  52. int getl(s,maxs,fd)        /* get one line from input file */
  53.   char    s[] ;            /* put it here in string form */
  54.   int    maxs ;            /* maximum length permitted */
  55.   FILE    *fd ;            /* file pointer for input file */
  56.   {                /* getl returns no. chars used in s */
  57.                 /*  ( or -1 if EOF reached */
  58.      int i ;
  59.                 /* get next line of input */
  60.      if( fgets(s,maxs-1,fd) == NULL )
  61.     return( -1 ) ;        /* EOF - return special length value */
  62.  
  63.      i = strlen(s) ;        /* get string length */
  64.      if(   s[i-1] != '\n' )     /* see if a new-line is present */
  65.     {  s[i] = '\n' ;        /*  n - append one */
  66.        s[i+1] = '\0' ;      /* restore end of string marker */
  67.        i = i + 1 ;        /* adjust string length */
  68.     }
  69.                 /* return length of line */
  70.      return(i+1) ;        /* count the '\o' at end too */
  71.   }
  72.  
  73.  
  74. int putl(s,fd)            /* output one line of text */
  75.   char    s[] ;            /* line to output in string form */
  76.   FILE    *fd ;            /* output file pointer */
  77.   {
  78.      return( fputs(s,fd) ) ;    /* use fputs libary function */
  79.   }
  80.  
  81.  
  82.  
  83.